﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExpressionCalculator
{
  public class Calculator
  {
    public enum Operators
    {
      add,
      subtract,
      multiply,
      divide
    };

    private double? _number1;
    public string Number1
    {
      get
      {
        if (_number1.HasValue)
          return _number1.ToString();
        else
          return String.Empty;
      }
      set
      {
        try
        {
          if (double.Parse(value) != _number1)
          {
            _number1 = double.Parse(value);
          }
        }
        catch
        {
          _number1 = null;
        }
      }
    }

    private double? _number2;
    public string Number2
    {
      get
      {
        if (_number2.HasValue)
          return _number2.ToString();
        else
          return String.Empty;
      }
      set
      {
        try
        {
          if (double.Parse(value) != _number2)
          {
            _number2 = double.Parse(value);
          }
        }
        catch
        {
          _number2 = null;
        }
      }
    }

    private Operators? _currOperator;
    public String CurrOperator
    {
      get
      {
        if (!_currOperator.HasValue)
        {
          return String.Empty;
        }
        else
        {
          switch (_currOperator)
          {
            case Operators.add:
              return "+";
              case Operators.subtract:
              return "-";
            case Operators.multiply:
              return "*";
            default:
              return string.Empty;
          }
        }
      }
      set
      {
        Operators? newOperators;
        switch (value)
        {
          case "+":
            newOperators = Operators.add;
            break;
          case "-":
            newOperators = Operators.subtract;
            break;
          case "*":
            newOperators = Operators.multiply;
            break;
          default:
            newOperators = null;
            break;
        }
        if (newOperators != _currOperator)
        {
          _currOperator = newOperators;
        }
      }
    }

    public string Result
    {
      get
      {
        if (_number1.HasValue && _number2.HasValue && _currOperator.HasValue)
        {
          switch (_currOperator)
          {
            case Operators.add:
              return Add(_number1.Value, _number2.Value).ToString();
            case Operators.multiply:
              return Multiply(_number1.Value, _number2.Value).ToString();
            default:
              return String.Empty;
          }
        }
        else
          return string.Empty;
      }
    }

    /// <summary>
    /// Adds the 2 params together
    /// </summary>
    /// <param name="num1"></param>
    /// <param name="num2"></param>
    /// <returns></returns>
    public double Add(double num1, double num2)
    {
        return num1 + num2;
    }

    /// <summary>
    /// Subtracts the 2 params together
    /// </summary>
    /// <param name="num1"></param>
    /// <param name="num2"></param>
    /// <returns></returns>
    public double Subtract(double num1, double num2)
    {
        return num1 - num2;
    }

    /// <summary>
    /// Multiplies the 2 params together
    /// </summary>
    /// <param name="num1"></param>
    /// <param name="num2"></param>
    /// <returns></returns>
    public double Multiply(double num1, double num2)
    {
      return num1 * num2;
    }

    internal void Reset()
    {
        _number1 = null;
        _number2 = null;
        _currOperator = null;
    }
  }
}
